home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
utility.lha
/
utility
/
pool.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
1KB
|
62 lines
// $Id: pool.C,v 1.3 91/09/06 17:04:25 dag Exp $
//
// Author:
//
// Dag Bruck, Department of Automatic Control, Lund Institute of Technology,
// Box 118, S-221 00 Lund, Sweden (dag@control.lth.se).
#include <pool.H>
const unsigned UNIT = sizeof(Pool :: Pool_Element);
Pool :: Pool(unsigned n)
: free_list(0), blockp(0), sz(((n + UNIT - 1) / UNIT) * UNIT)
{
alloc_block();
}
Pool :: ~Pool()
{
register Pool_Block* b;
while (blockp != 0) {
b = blockp->next;
delete blockp;
blockp = b;
}
}
void Pool :: alloc_block()
{
register unsigned n = (UNIT*1024) / sz;
// The number of elements to allocate in a block
// Allocate a new block of elements, and insert it into a list
// so it can be released when the pool is destroyed.
register Pool_Block* blk =
(Pool_Block *) new char[sizeof(Pool_Block) + n * sz];
blk->next = blockp;
blockp = blk;
// Link together all elements in the new block.
register Pool_Element* pe = &blk->pe;
while (--n > 0) {
pe->next = (Pool_Element *) ((char *) pe + sz);
pe = pe->next;
}
// The last element of the block points to the existing free list,
// which normally is empty (but needn't be). The new free list
// starts with the first element of the newly allocated block.
pe->next = free_list;
free_list = &blk->pe;
}